package com.manor.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.orhanobut.logger.Logger;
import com.manor.main.R;
import com.manor.main.bean.OnlineUserBean;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class AutoPollAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private final Context mContext;
    //    private final List<String> mData;
    private List<OnlineUserBean> onlineUserBeanList = new ArrayList<>();
    Animation rotateAnim = null;
    public AutoPollAdapter(Context context, List<OnlineUserBean> list) {
        this.mContext = context;
        this.onlineUserBeanList = list;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.rv_onlineusername_item, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }



    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
//        String data = mData.get(position%mData.size());

        holder.setText(R.id.tv_name, onlineUserBeanList.get(position%onlineUserBeanList.size()).getName());

        int width = holder.getView(R.id.tv_name).getWidth();
        int height = holder.getView(R.id.tv_name).getHeight();

        ImageView imgBg = holder.getView(R.id.iv_img_bg);
        FrameLayout flName = holder.getView(R.id.fl_name);
        TextView tvName = holder.getView(R.id.tv_name);
        //文字w h width height


        int viewHeight = holder.itemView.getHeight();
        int viewWidth = holder.itemView.getWidth();
        int setHeight= (int) (width*1f)+tvName.getPaddingLeft()+tvName.getPaddingRight();
        if (setHeight-viewWidth>0&&setHeight-viewHeight>0)
            setHeight= (int) (viewWidth);


        holder.getView(R.id.iv_img_bg).getLayoutParams().width=setHeight;
        holder.getView(R.id.iv_img_bg).getLayoutParams().height=setHeight;
        holder.getView(R.id.iv_img_bg).setPadding(10,10,10,10);
        holder.getView(R.id.iv_img_bg).setAlpha(0.8f);





        holder.getView(R.id.iv_img_bg).setBackgroundResource(R.mipmap.find_content_bg);

//        holder.getView(R.id.iv_img_bg)

        Logger.e("MI width=="+width+",height=="+height+",text="+ ((TextView) holder.getView(R.id.tv_name)).getText());
        Logger.e("viewHeight=="+viewHeight+",viewWidth=="+viewWidth);




//        Logger.e("height=="+height);
//        Logger.e("width=="+width);

//        holder.getView(R.id.iv_img_bg).getLayoutParams().width=swidth;
//        holder.getView(R.id.iv_img_bg).getLayoutParams().height=width;

        rotateAnim = AnimationUtils.loadAnimation(mContext, R.anim.view_anim_rotate);
        rotateAnim.setRepeatCount(1);
        imgBg.startAnimation(rotateAnim);

    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }
}
